﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ScoreFormatter.cs" company="Microsoft">
//   Microsoft
// </copyright>
// <summary>
//   Defines the ScoreFormatter type.
// </summary>
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// --------------------------------------------------------------------------------------------------------------------

namespace MySemanticSearch.Formatters
{
    using System;
    using System.Globalization;
    using System.Windows.Data;

    /// <summary>
    /// This class implements the IValueConverter class to convert doubles for the datagrid
    /// </summary>
    public class ScoreFormatter : IValueConverter
    {
        /// <summary>
        /// Converts scores into expected values for the Data Grid.
        /// </summary>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <param name="targetType">
        /// The target type.
        /// </param>
        /// <param name="parameter">
        /// The parameter.
        /// </param>
        /// <param name="culture">
        /// The culture.
        /// </param>
        /// <returns>
        /// A converted double
        /// </returns>
        /// <exception cref="NotImplementedException">
        /// </exception>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var scoreValue = (double)value;

            if (scoreValue == 0)
            {
                return null;
            }

            return scoreValue;
        }

        /// <summary>
        /// Converts back the result given by the View
        /// </summary>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <param name="targetType">
        /// The target type.
        /// </param>
        /// <param name="parameter">
        /// The parameter.
        /// </param>
        /// <param name="culture">
        /// The culture.
        /// </param>
        /// <returns>
        /// A converted value to be handled by the ViewModel
        /// </returns>
        /// <exception cref="NotImplementedException">
        /// </exception>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return 0;
            }

            return value;
        }
    }
}
